package com.cxh.androidmedia.activity;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.ContentProviderClient;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.widget.TextView;

import com.cxh.androidmedia.R;
import com.cxh.androidmedia.base.BaseActivity;
import com.cxh.androidmedia.utils.CCLog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Cxh
 * Time : 2020-06-05  11:46
 * Desc : 一款短视频App，断点拍摄、添加水印、本地转码，视频剪辑、视频拼接、MV特效等
 */
public class MediaMainActivity extends BaseActivity {

    private static final String TEST_PROVIDER = "content://com.example.helloworld.test.myprovider";

    @BindView(R.id.tvMessage)
    TextView tvMessage;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_media_x264;
    }

    @Override
    protected void init() {

        bindService(new Intent(), new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                try {
                    service.linkToDeath(new IBinder.DeathRecipient() {
                        @Override
                        public void binderDied() {

                        }
                    }, 0);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {

            }
        }, 0);
    }

    @Override
    @OnClick({R.id.buttonData, R.id.buttonReceiver})
    public void onViewClick(View view) {
        super.onViewClick(view);
        switch (view.getId()) {
            case R.id.buttonData:
                useContentProvider();
                break;

            case R.id.buttonReceiver:
                useReceiver();
                break;
        }
    }

    @SuppressLint("WrongConstant")
    private void useReceiver() {
        Intent intent = new Intent("com.example.helloworld.test.receiver");
        intent.putExtra("key", "Fuck You!");
        intent.addFlags(0x01000000);
        sendOrderedBroadcast(intent, null);
    }

    private void useContentProvider() {
        Uri uri = Uri.parse(TEST_PROVIDER);

        ContentProviderClient client = getContentResolver().acquireUnstableContentProviderClient(uri);
        if (null != client) {
            try {
                Cursor cursor = client.query(uri, null, null, null, null);

                if (null != cursor) {

                    if (cursor.moveToNext()) {
                        String message = cursor.getString(0);

                        CCLog.i("receive data: " + message);

                        postMessage(message);
                    }
                    cursor.close();
                } else {
                    CCLog.e("fail to query!");
                }

                client.release();
            } catch (RemoteException e) {
                e.printStackTrace();
                CCLog.e("fail to query! e: " + e.getMessage());
            }
        }else {
            CCLog.e("fail to get provider client!");
        }
    }

    private void postMessage(String message) {
        tvMessage.setText(message);
    }
}
